<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/13
 * Time: 16:33
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Attestation extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 展示
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where['user_del'] = 1;
            $where['user_type'] = ['>',1];
            //状态
            if(isset($params['user_status']) && $params['user_status'] != ''){
                $where['user_status'] = $params['user_status'];
            }
            //用户名
            if(isset($params['user_username']) && $params['user_username'] != ''){
                $where['user_username'] = ['like', '%' . $params['user_username'] . '%'];
            }
            //用户名
            if(isset($params['user_type']) && $params['user_type'] != ''){
                $where['user_type'] = $params['user_type'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->field('user_image, user_username, user_id, user_reject_time, user_status, user_zheng, 
                user_fan, user_card, user_zhizhao, user_type, user_shop')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_status asc,user_reject_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * @return mixed|\think\response\Json
     * 经纪人审核
     */
    public function userAttestation(){
        if(Request::instance()->isPost()){
            $user_id = input('user_id');
            $user_status = input('user_status');
            $update = [
                'user_status' => $user_status,
                'user_reject_time' => date('Y-m-d H:i:s')
            ];
            if($user_status == 2){
                $user_reject_reason = trim(input('user_reject_reason'), '');
                if(mb_strlen($user_reject_reason, 'UTF8') > 100){
                    return json(['code' => 100, 'msg' => '驳回理由不可超过100字']);
                }
                $update['user_reject_reason'] = input('user_reject_reason');
            }
            if($user_status == 1){
                $user_type = Db::table('up_u_user')->where('user_id', $user_id)->value('user_type');
                if($user_type == 2){
                    //经纪人认证之后享受10天的季度会员
                    $today = date('Y-m-d');
                    $today_10 = date("Y-m-d",(strtotime("{$today}")+3600*24*10));
                    $update['user_auth'] = 2;
                    $update['user_auth_start'] = $today;
                    $update['user_auth_end'] = $today_10;
                }
            }
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $user_id = input('id');
        $userInfo = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_id, user_username, user_image, user_status, user_reject_reason, user_reject_time, 
            user_zheng, user_fan, user_card, user_zhizhao, user_shop')
            ->find();
        $this->assign('userInfo', $userInfo);
        return $this->fetch('attestation/userattestation');
    }
    
    /**
     * @return mixed
     * 经纪人详情
     */
    public function userDetail(){
        $user_id = input('id');
        $userInfo = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_id, user_username, user_image, user_status, user_reject_reason, user_reject_time, 
            user_zheng, user_fan, user_card, user_zhizhao, user_shop')
            ->find();
        
        $this->assign('userInfo', $userInfo);
        return $this->fetch('attestation/userdetail');
    }
}